package com.novunda.platform.config;

import com.google.common.collect.Lists;
import com.novunda.platform.common.utils.PWDUtils;
import com.novunda.platform.entities.Dict;
import com.novunda.platform.entities.Menu;
import com.novunda.platform.entities.Office;
import com.novunda.platform.entities.Role;
import com.novunda.platform.entities.User;
import com.novunda.platform.services.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * @author linfeng
 */
@Component
public class DBInitializer {

    private static final Logger LOG = LoggerFactory.getLogger(DBInitializer.class);

    @Autowired
    private UserService userService;

    @Autowired
    private PlatformService platformService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private OfficeService officeService;

    @Autowired
    private DictService dictService;

    @Value("${init-db:false}")
    private String initDatabase;


    @PostConstruct
    public void init() {
        try {
            LOG.info("############## InitDatabase :{} ########################", initDatabase);
            if (Boolean.parseBoolean(initDatabase)) {
                initDatabase();
            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }

    void initDatabase() {

        LOG.info("Initializing Database with sample data");

        User user1 = new User(1L, "admin", PWDUtils.entryptPassword("admin"), "Administrator");
        user1.setState(0);
        user1.setCreateBy(user1);
        user1.setUpdateBy(user1);
        user1.setLoginDate(new Date());
        user1.setLoginIp("127.0.0.1");

        userService.saveOnce(user1);

        initDictTable(user1);

        Office officeParant = new Office(0L);
        Office office = new Office(1L, officeParant, "根组织", "100", "1");
        office.setCreateBy(user1);
        office.setUpdateBy(user1);
        officeService.saveOnce(office);

        Office office1 = new Office(2L, office, "上海金融云集团", "1000", "1");
        office1.setCreateBy(user1);
        office1.setUpdateBy(user1);
        officeService.saveOnce(office1);

        user1.setCompany(office1);
        user1.setOffice(office1);
        userService.saveOnce(user1);

        List<Office> officeList = Lists.newArrayList(office1);
        Role role = new Role(1L, "管理员", "admin");
        role.setCreateBy(user1);
        role.setUpdateBy(user1);
        role.setOfficeList(officeList);
        role.setOffice(office1);
        platformService.saveOnce(role);

        List<Role> roleList = Lists.newArrayList(role);
        user1.setRoleList(roleList);
        userService.saveOnce(user1);

        Menu parant = new Menu(0L);
        Menu menu = new Menu(1L, parant, "顶级菜单", "", "", roleList, user1);
        menu.setCreateBy(user1);
        menu.setUpdateBy(user1);
        menu = menuService.saveOnce(menu);

        Menu menu1 = new Menu(2L, menu, "首页", "/", "", roleList, user1);
        Menu menu2 = new Menu(3L, menu, "系统设置", "#", "", roleList, user1);
        Menu menu3 = new Menu(4L, menu2, "菜单管理", "/sys/menu", "", roleList, user1);
        Menu menu4 = new Menu(5L, menu2, "用户管理", "/", "", roleList, user1);
        Menu menu5 = new Menu(6L, menu2, "角色管理", "/sys/role", "", roleList, user1);
        Menu menu6 = new Menu(7L, menu2, "机构管理", "/sys/office", "", roleList, user1);
        Menu menu7 = new Menu(8L, menu2, "字典管理", "/sys/dict", "", roleList, user1);
        List<Menu> menuArrayList = Lists.newArrayList(menu1, menu2, menu3, menu4, menu5, menu6);
        menuService.saveMultiMenu(menuArrayList);

        List<Menu> menuList = Lists.newArrayList(menu);
        List<User> userList = Lists.newArrayList(user1);
        role.setUserList(userList);
        role.setMenuList(menuList);
        platformService.saveRole(role);


    }

    public void initDictTable(User user1) {

        List<Dict> saveList = Lists.newArrayList();
        saveList.add(getDict(user1, "显示", "1", "show_hide", 10, "是否可见"));
        saveList.add(getDict(user1, "隐藏", "0", "show_hide", 20, "是否可见"));
        saveList.add(getDict(user1, "否", "0", "sys_shortcut_show", 10, "是否添加到下拉菜单"));
        saveList.add(getDict(user1, "是", "1", "sys_shortcut_show", 20, "是否添加到下拉菜单"));
        saveList.add(getDict(user1, "公司", "1", "sys_office_type", 10, "机构类型"));
        saveList.add(getDict(user1, "部门", "2", "sys_office_type", 20, "机构类型"));
        saveList.add(getDict(user1, "一级", "1", "sys_office_grade", 20, "机构等级"));
        saveList.add(getDict(user1, "二级", "2", "sys_office_grade", 30, "机构等级"));
        saveList.add(getDict(user1, "所有数据", "1", "sys_data_scope", 10, "数据范围"));
        saveList.add(getDict(user1, "所在公司及以下数据", "2", "sys_data_scope", 20, "数据范围"));
        saveList.add(getDict(user1, "所在公司数据", "3", "sys_data_scope", 30, "数据范围"));
        saveList.add(getDict(user1, "所在部门及以下数据", "4", "sys_data_scope", 40, "数据范围"));
        saveList.add(getDict(user1, "所在部门数据", "5", "sys_data_scope", 50, "数据范围"));
        saveList.add(getDict(user1, "仅本人数据", "8", "sys_data_scope", 60, "数据范围"));
        saveList.add(getDict(user1, "按明细设置", "9", "sys_data_scope", 70, "数据范围"));

        dictService.saveMulit(saveList);

    }

    private Dict getDict(User user1, String label, String value, String grade, int sort, String level) {
        Dict dict8 = new Dict();
        dict8.setLabel(label);
        dict8.setValue(value);
        dict8.setType(grade);
        dict8.setSort(sort);
        dict8.setDescription(level);
        dict8.setCreateBy(user1);
        dict8.setUpdateBy(user1);
        return dict8;
    }

}
